import {action, computed, observable} from "mobx";
import {Bubble, Toast, Tools} from "weaver-mobile";
import {Browser, BrowserHrm, ListViewPage} from "weaver-mobile-page";
import {programApi} from "../../api/performance/programApi";
import {cycleDataApi} from "../../api/performance/accessResultApi";

const getLabel = Tools.getLabel;

/**
 * 方案设置查询
 * jiansong 2019/11/19
 */
export default class ProgramStore {

    @observable list = new ListViewPage.Store({route: "/performance/program"});
    @observable subBrowser = new Browser.Store({route: "/performance/program/condition"});
    @observable deptBrowser = new Browser.Store({route: "/performance/program/condition"});
    @observable hrmBrowser = new BrowserHrm.Store({route: "/performance/program/condition"});
    @observable subBrowserValue = [];
    @observable deptBrowserValue = [];
    @observable hrmBrowserValue = [];
    @observable stabList = []; //考核周期列表
    @observable hasInit=false; //是否已经初始化

    dataKey = ""; //获取列表数据key

    @observable queryParams = { //查询条件
        type: "", //类型
        hrmids: "", //人员
        subcompanyids: "", //分部
        departmentids: "", //部门
    };

    @computed get queryType() { //类型
        let type = [{key: "", title: getLabel(332, '全部'), showname: getLabel(332, '全部')}];
        this.stabList.forEach(item => {
            item.key == 1 && type.push({
                key: "nomonth",
                title: getLabel(509524, "无月度"),
                showname: getLabel(509524, "无月度")
            });
            item.key == 2 && type.push({
                key: "noquarter",
                title: getLabel(508861, "无季度"),
                showname: getLabel(508861, "无季度")
            });
            item.key == 3 && type.push({
                key: "nohyear",
                title: getLabel(508862, "无半年"),
                showname: getLabel(508862, "无半年")
            });
            item.key == 4 && type.push({
                key: "nofyear",
                title: getLabel(508865, "无年度"),
                showname: getLabel(508865, "无年度")
            });
        });
        return type;
    };

    @action("初始化操作")
    doInit = () => {
        this.queryParams = {
            type: "",
            hrmids: "",
            subcompanyids: "",
            departmentids: "",
        };
        this.subBrowserValue = [];
        this.deptBrowserValue = [];
        this.hrmBrowserValue = [];
        this.initData();
    }

    @action("搜索条件改变")
    onSearchChange = (params) => {
        for (let key in params) {
            this.queryParams[key] = params[key];
        }
    }

    @action("重置条件")
    resetCondition = () => {
        this.queryParams = {
            type: "",
            hrmids: "",
            subcompanyids: "",
            departmentids: "",
        };
        this.subBrowserValue = [];
        this.deptBrowserValue = [];
        this.hrmBrowserValue = [];
    }

    @action("更新store数据")
    setState = (params = {}) => {
        Object.keys(params).forEach((key) => {
            this[key] = params[key];
        });
    }

    @action("获取考核周期数据")
    initData = () => {
        cycleDataApi().then(action(res => {
            if (res.api_status) {
                this.stabList = res.stabList;
                this.hasInit=true;
                this.getTableDatas();
            } else {
                Bubble.error(res.msg || getLabel(508818, "接口调用失败！"))
            }
        }))
    };

    @action("渲染table数据")
    getTableDatas = () => {
        programApi(this.queryParams).then(action(res => {
            if (res.api_status) {
                this.list.clearData();
                const {datas: dataKey} = res;
                this.dataKey = dataKey;
                this.list.getData({dataKey});
            } else {
                Bubble.error(res.msg || getLabel(508818, "接口调用失败！"))
            }
        }));
    };

    @action('更新列表数据数据')
    updateListData = (params) => {
        const {data, updateDataSource, setState} = this.list;
        let index = data.findIndex(item => item.hrmid == params.resourceid);
        if (index!=undefined) {
            Tools.callApi({
                url: '/api/ec/dev/table/datas',
                method: 'POST',
                params: {dataKey: this.dataKey, current: index + 1, pageSize: 1},
            }).then(res => {
                if (res.datas && res.datas[0]) {
                    data[index] = res.datas[0];
                    setState({data});
                    updateDataSource();
                }
            })
        }
    }

}